package br.com.devteam.sgap.view.interno;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import br.com.devteam.base.util.Message;
import br.com.devteam.base.util.SGAPContantes;
import br.com.devteam.sgap.model.Perfil;
import br.com.devteam.sgap.model.Usuario;
import br.com.devteam.sgap.service.IService;

@Controller(value = "beanSessaoUsuario")
@Scope(value = "session")
@ManagedBean
public class BeanSessaoUsuario {

	@Autowired
	private IService service;

	private String login, senha, confirmacaoDeSenha;

	private Usuario loggedUser;
	private Usuario userUpdate;

	public BeanSessaoUsuario() {
	}

	public Usuario getUsuario() {

		if (loggedUser == null) {

			Authentication auth = ((SecurityContext) SecurityContextHolder
					.getContext()).getAuthentication();

			if (auth != null) {
				try {
					Map<String, Object> params = new HashMap<String, Object>();
					params.put("login", auth.getName());
					List<Usuario> users = service.findByNamedQuery(
							Usuario.QUERY_LIST_BY_LOGIN, params);
					if (users.size() > 0)
						loggedUser = users.get(0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		return loggedUser;
	}

	public String actionAlterarDados() {

		userUpdate = new Usuario();
		userUpdate.setId(getUsuario().getId());
		userUpdate.setEnable(getUsuario().getEnable());
		userUpdate.setLogin(getUsuario().getLogin());
		userUpdate.setNome(getUsuario().getNome());
		userUpdate.setPerfis(new ArrayList<Perfil>());
		userUpdate.getPerfis().addAll(getUsuario().getPerfis());
		userUpdate.setSenha(getUsuario().getSenha());

		return SGAPContantes.PAGE_ALTERAR_USUARIO;
	}

	public String actionSalvar() {

		if (validar()) {

			try {
				/*for (Perfil perfil : userUpdate.getPerfis()) {
					perfil.setLogin(userUpdate.getLogin());
				}*/
				service.update(userUpdate);

				loggedUser = userUpdate;
				userUpdate = null;

				Message.i(SGAPContantes.MESSAGE_FIELD, SGAPContantes.ENTRY_UPDATE);
				Message.manterMensagensRedirect();

				return SGAPContantes.PAGE_VISUALIZAR_DADOS;
			} catch (Exception e) {
				e.printStackTrace();
				Message.e(SGAPContantes.MESSAGE_FIELD,
						SGAPContantes.ERROR_GENERIC);
			}
		}

		return "";
	}

	private boolean validar() {
		boolean retorno = true;

		if (!retorno) {
			Message.e(SGAPContantes.MESSAGE_FIELD,
					SGAPContantes.ERROR_NOT_DELETED);
		}

		return retorno;
	}
	
	public boolean isUsuarioLogado() {
		return getUsuario() != null;
	}
	
	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}
	
	public String getSenha() {
		return senha;
	}
	
	public void setSenha(String senha) {
		this.senha = senha;
	}

	public String getConfirmacaoDeSenha() {
		return confirmacaoDeSenha;
	}

	public void setConfirmacaoDeSenha(String confirmacaoDeSenha) {
		this.confirmacaoDeSenha = confirmacaoDeSenha;
	}

	public Usuario getUserUpdate() {
		return userUpdate;
	}

	public void setUserUpdate(Usuario userUpdate) {
		this.userUpdate = userUpdate;
	}

}
